package com.yundong.gongchengji.ui.taskhall.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.TextView;

import com.yundong.gongchengji.R;
import com.yundong.gongchengji.adapter.MyPagerAdapter;
import com.yundong.gongchengji.base.BaseActivity;
import com.yundong.gongchengji.base.BaseFragment;
import com.yundong.gongchengji.ui.taskhall.fragment.TaskApplicantFragment;
import com.yundong.gongchengji.ui.taskhall.fragment.TaskDetailFragment;
import com.yundong.gongchengji.ui.taskhall.fragment.TaskHudongFragment;
import com.yundong.gongchengji.ui.taskhall.fragment.TaskReleaseFragment;
import com.yundong.gongchengji.util.TimeTools;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
 * 任务大厅
 * Created by ls on 2017/2/21.
 */


public class TaskHallActivity extends BaseActivity {
    private TabLayout tabTaskfragmentTitle;
    private ViewPager viewpageTaskfragment;
    //创建一个标题集合
    private List<String> title_list=new ArrayList<String>();
    //fragment集合
    private List<BaseFragment>fragment_list=new ArrayList<BaseFragment>();
    private TextView textJishi;

    private Timer mTimer;
    private TimerTask mTimerTask;
    private long timeJiShi;//计时的时间
    @Override
    public int getContentViewId() {
        return R.layout.activity_task_hall;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setLeft();
        setTitle("任务大厅");
        tabTaskfragmentTitle= (TabLayout) findViewById(R.id.tab_taskfragment_title);
        viewpageTaskfragment= (ViewPager) findViewById(R.id.viewpage_taskfragment);
        textJishi= (TextView) findViewById(R.id.text_jishi);
    }

    @Override
    protected void initVariable() {
        timeJiShi=86400000+10000;

        title_list.add("任务详情");
        title_list.add("任务互动");
        title_list.add("申请企业");
        title_list.add("发布方");
        fragment_list.add(new TaskDetailFragment());
        fragment_list.add(new TaskHudongFragment());
        fragment_list.add(new TaskApplicantFragment());
        fragment_list.add(new TaskReleaseFragment());
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        start();
        tabTaskfragmentTitle.setTabMode(TabLayout.MODE_FIXED);
        tabTaskfragmentTitle.addTab(tabTaskfragmentTitle.newTab().setText(title_list.get(0)));
        tabTaskfragmentTitle.addTab(tabTaskfragmentTitle.newTab().setText(title_list.get(1)));
        tabTaskfragmentTitle.addTab(tabTaskfragmentTitle.newTab().setText(title_list.get(2)));
        tabTaskfragmentTitle.addTab(tabTaskfragmentTitle.newTab().setText(title_list.get(3)));
        viewpageTaskfragment.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragment_list.get(position);
            }

            @Override
            public int getCount() {
                return fragment_list.size();
            }
        });
        MyPagerAdapter adapter=new MyPagerAdapter(getSupportFragmentManager(),title_list,fragment_list);
        viewpageTaskfragment.setAdapter(adapter);
        tabTaskfragmentTitle.setupWithViewPager(viewpageTaskfragment);
//        tabTaskfragmentTitle.setTabsFromPagerAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimer!=null){
            mTimer.cancel();
        }
    }
    private void start(){
        if (mTimer!=null){
            mTimer.cancel();
        }
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        timeJiShi-=1000;
                        Log.d("Task", "下一个");
                        chuliTime(TimeTools.getNewTiming(timeJiShi));
                    }
                });

            }
        };
        mTimer = new Timer();
        mTimer.schedule(mTimerTask, 0, 1000);

    }
    public void chuliTime(String string){
        SpannableString spannableString = new SpannableString(string);
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(Color.parseColor("#FE5C5C"));
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(Color.parseColor("#FE5C5C"));
        ForegroundColorSpan colorSpan3 = new ForegroundColorSpan(Color.parseColor("#FE5C5C"));
        ForegroundColorSpan colorSpan4 = new ForegroundColorSpan(Color.parseColor("#FE5C5C"));
        spannableString.setSpan(colorSpan1, 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(colorSpan2, 3, 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(colorSpan3, 6, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(colorSpan4, 9, 11, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textJishi.setText(spannableString);
    }
}
